Skip to content

fix(schema): include defaults in ~standard.jsonSchema.input - #1695

Open
ybai08 wants to merge 1 commit into
arktypeio:mainfrom
ybai08:fix/standard-json-schema-input-defaults
Open

ybai08 wants to merge 1 commit into
arktypeio:mainfrom
ybai08:fix/standard-json-schema-input-defaults

Conversation

@ybai08

@ybai08 ybai08 commented Oct 17, 2026

Copy link
Copy Markdown

Fixes #1575

~standard.jsonSchema.input() converted this.rawIn, and OptionalNode.rawIn (like the defaultables reduceIo for tuples) drops the default, so a type like

type({ firstName: 'string = "First"' })["~standard"].jsonSchema.input({ target: "draft-07" })

produced { properties: { firstName: { type: "string" } } } with no default, even though toJsonSchema() on the same type includes it. Zod and Valibot both include default in their input schemas, and for input it is where JSON Schema's default annotation applies (the key may be omitted and the default is used).

The input schema now converts the type itself with a new internal io: "in" on the JSON Schema context, which keeps rawIn's behavior in the two places where converting the full type would differ:

  • a morph converts its input (declaredIn ?? in) directly instead of going through the morph fallback, so a configured morph fallback still isn't used for input (as before, since rawIn had no morphs), and a default inside a morph's input is kept
  • onUndeclaredKey("delete") doesn't add additionalProperties: false, since those keys are allowed on input

Defaults that aren't JSON still go through the defaultValue fallback, which the standard converter already maps to omitting them.

I avoided keeping the default on rawIn itself (e.g. as meta on the input prop) because that changes T.in's hash, so T.in.equals(type({ "a?": "string" })) would become false.

One cosmetic difference I noticed: for a union whose true branch is a morph, e.g. type("true").pipe(() => 1).or("false"), the input schema is now { enum: [true, false] } rather than { type: "boolean" } (the boolean grouping only matches unit branches). Happy to special-case that if you'd prefer.

Tests

Added to ark/type/__tests__/standardSchema.test.ts:

  • defaults on object props (including a thunk default) appear in the input schema, and the output schema is unchanged
  • defaults on tuple elements and inside a morph's input (string.numeric.parse = '1') appear in the input schema
  • onUndeclaredKey("delete") allows undeclared keys in the input schema but not the output
  • a configured morph fallback isn't used for the input schema

The first two fail on main and pass with this change; the last two pass on both and guard the rawIn parity.

What was run locally:

  • pnpm testFiles ark/type/__tests__/standardSchema.test.ts (with type assertions): 17 passing
  • pnpm test: 1795 passing
  • pnpm typecheckRepo: no errors
  • prettier --check and eslint on the changed files: clean

Not run: the full pnpm prChecks (buildRepo, benches, testV8, testIntegration, testTsVersions, testTypedLoose).

The input schema was generated from rawIn, which drops default values
from optional props and tuple elements, so defaults set with
`key: "string = 'x'"` never reached Standard JSON Schema consumers.

Convert the type itself with io: "in" instead, converting a morph's
input directly and allowing undeclared keys that would be deleted, so
the result matches rawIn apart from the defaults.

Fixes arktypeio#1575

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

This reviews the rework of ~standard.jsonSchema.input to convert the full type with a new internal io: "in" context flag instead of rawIn, so defaults survive into the input schema.

  • Input conversion via io: "in" — BaseRoot["~standard"].jsonSchema.input now converts the type itself with io: "in" (ark/schema/roots/root.ts:112), and MorphNode.innerToJsonSchema converts declaredIn ?? inner.in directly under that flag (ark/schema/roots/morph.ts:207), preserving defaults a rawIn reduce would have dropped. Verified against base: a nested object default inside a morph input now appears in the input schema.
  • Undeclared-key parity — StructureNode only emits additionalProperties: false for onUndeclaredKey("delete") outside io: "in" (ark/schema/structure/structure.ts:921), keeping rawIn's input behavior. Confirmed unchanged versus base for a transform-backed delete.
  • Tests — five new cases in ark/type/__tests__/standardSchema.test.ts cover object/thunk/tuple defaults, defaults inside a morph's input, onUndeclaredKey("delete") input vs output, and morph-fallback parity. Full pnpm test (1795 passing) and the targeted schema suites pass locally.

The union-of-unit-branches case flagged in the description (type("true").pipe(() => 1).or("false") now emitting { enum: [true, false] } instead of { type: "boolean" }) produces a valid, equivalent schema for both targets, so I don't think it warrants a special case.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

ssalbdivad added a commit that referenced this pull request Oct 22, 2026
~standard.jsonSchema.input converted the Type's rawIn, which strips
defaults, so an input schema never had a key's `default` (#1575). Zod and
Valibot both write them, and consumers like form generators read them.

This ports external PR #1695's approach, written against main's
ark/schema JSON Schema code, to featherduster's ark/sets toJsonSchema.ts:

- ToJsonSchema.Context gains `io?: "in"`, which toStandardJsonSchema sets
  for `input` and which mergeToJsonSchemaConfigs carries like any option.
- `input` converts the Type itself rather than rawIn, so prop and tuple
  defaults are written by the existing writeDefault paths.
- With io "in", a morph writes its declaredIn or in directly instead of
  calling the morph fallback, so a configured morph handler doesn't apply
  to an input schema. An index key's morph likewise skips the fallback.
- With io "in", `"+": "delete"` no longer writes additionalProperties:
  false, or the propertyNames/additionalProperties pair of a lone index
  signature, since the input may have the keys delete removes. "reject"
  still does.

`output` and toJsonSchema() don't set io and are unchanged, and
precompiled validators bake `~standard.jsonSchema` by calling it, so they
bake the new input schema. The tests are #1695's, adapted to this
branch's API.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
ssalbdivad added a commit that referenced this pull request Nov 9, 2026
…uthors

Each fix below landed on featherduster from a port or an independent fix,
but each started from a reasonable community PR against 2.x, and the
CHANGELOG thanks every contributor whose work this release supersedes:

- #1721 (@RaphaelFakhri): fast-check keeps a regex's flags (#1720)
- #1722 (@louisss1016): Temporal values survive a morph / deepClone (#1719)
- #1725 (@RClaudiuM): an undefined discriminant case checks its key (#1724)
- #1715 (@SynthLuvr): ~standard.jsonSchema.input drops undefined (#1462);
  only its input half was taken, output keeps routing to fallback.unit

#1718 is not credited: 3.0 answers #1469 with jsonSchema meta rather than
its bare pattern key. #1705 and #1695 were already thanked.

Also notes that a cyclic index signature with no way out now generates
shrinking dictionaries instead of overflowing the stack (6f4dd5d).
@ssalbdivad ssalbdivad mentioned this pull request Nov 9, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To do

Development

Successfully merging this pull request may close these issues.

Standard JSON Schema Default Values

1 participant